import type { MutationTree, ActionTree, GetterTree } from 'vuex'
import type { IRootState } from './index'
import { Actions, Mutations } from '@/enum/city'

export interface ICityState {
  cityName: string
  cityCode: string
}

export const state: () => ICityState = () => ({
  cityName: '重庆市',
  cityCode: '500000',
})

export const mutations: MutationTree<ICityState> = {
  [Mutations.SET_CITY](state, { name, code }) {
    state.cityName = name
    state.cityCode = code
  },
}

export const actions: ActionTree<ICityState, IRootState> = {
  [Actions.getCity]({ commit }) {
    // 接口获取当前定位城市
    // 获取不到默认为重庆

    commit(Mutations.SET_CITY, {
      name: '重庆市',
      code: '500000',
    })
  },
}

export const getters: GetterTree<ICityState, IRootState> = {
  cityName: (state) => state.cityName,
  cityCode: (state) => state.cityCode,
}
